package cn.otrue.patienthealthmanager.adapter;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.activity.DoctorEvaluationActivity;
import cn.otrue.patienthealthmanager.bean.ChatModle;
import cn.otrue.patienthealthmanager.db.DBTools;
import cn.otrue.patienthealthmanager.http.HttpServer;
import cn.otrue.patienthealthmanager.utils.CommonUtil;
import cn.otrue.patienthealthmanager.utils.DensityUtil;
import cn.otrue.patienthealthmanager.utils.MyConst;

import com.jauker.widget.BadgeView;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.db.sqlite.WhereBuilder;
import com.lidroid.xutils.exception.DbException;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

public class DoctorCharlieAdpater extends BaseAdapter {

	public void ImageSet(String imgurl, ImageView img_headPhoto) {

		DisplayImageOptions options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.iv_user_photo)
				.showImageOnFail(R.drawable.iv_user_photo)
				.showImageForEmptyUri(R.drawable.iv_user_photo)
				.cacheInMemory(true)
				.cacheOnDisk(true)
				.bitmapConfig(Bitmap.Config.RGB_565)
				.displayer(
						new RoundedBitmapDisplayer(DensityUtil.dip2px(context,
								40))).build();

		ImageLoader.getInstance().displayImage(imgurl, img_headPhoto, options);
	}

	private List<Map<String, String>> data;
	private LayoutInflater inflater;
	private Context context;

	public DoctorCharlieAdpater(Context context, List<Map<String, String>> data) {
		super();
		this.data = data;
		this.context = context;
		inflater = LayoutInflater.from(this.context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		arg1 = inflater.inflate(R.layout.item_doctor_chat, null);
		final Map<String, String> eachMap = data.get(arg0);
		TextView tw_doctor_char_name = (TextView) arg1
				.findViewById(R.id.tv_doctor_char_name);
		TextView tv_doctor_char_lasttime = (TextView) arg1
				.findViewById(R.id.tv_doctor_char_lasttime);
		TextView tv_doctor_char_evaluation = (TextView) arg1
				.findViewById(R.id.tv_doctor_char_evaluation);
		ImageView iv_head_image = (ImageView) arg1
				.findViewById(R.id.iv_head_image);
		TextView tw_doctor_last = (TextView) arg1
				.findViewById(R.id.tw_doctor_last);

		tv_doctor_char_evaluation.setVisibility(View.VISIBLE);
		BadgeView badgeView = new BadgeView(context);
		badgeView.setTargetView(iv_head_image);
		if (eachMap.containsKey("unreadNum")) {
			if (!TextUtils.isEmpty(eachMap.get("unreadNum"))) {

				badgeView.setBadgeCount(Integer.parseInt(eachMap
						.get("unreadNum")));
			}
		}

		ImageSet(eachMap.get("headimg"), iv_head_image);

		try {
			ChatModle chatModle = DBTools.db.findFirst(Selector
					.from(ChatModle.class)
					.where("inUser", "=", eachMap.get("aliasAccount"))
					.and(WhereBuilder.b("outUser", "=", MyConst.getACCOUNT()))
					.orderBy("createTime", true));

			if (chatModle != null) {
				if (TextUtils.isEmpty(chatModle.getFilePath())) {
					tw_doctor_last.setText(chatModle.getMessage());
				} else {

					if (chatModle
							.getFilePath()
							.substring(
									chatModle.getFilePath().lastIndexOf(".") + 1)
							.equals("acc")) {
						tw_doctor_last.setText("音频文件");
					} else {
						tw_doctor_last.setText("图片文件");
					}
				}

				tv_doctor_char_lasttime.setText(CommonUtil.getFormatDate(
						"MM-dd hh:mm", chatModle.getCreateTime()));
			} else {
				tw_doctor_last.setText("");
				tv_doctor_char_lasttime.setText("");
			}
		} catch (DbException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		tv_doctor_char_evaluation.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intetn = new Intent(context,
						DoctorEvaluationActivity.class);
				intetn.putExtra("data", HttpServer.gson.toJson(eachMap));
				context.startActivity(intetn);
			}
		});

		if (eachMap.containsKey("state") && eachMap.get("state").equals("0")) {
			// tw_doctor_char_msg.setText("在线");
			tw_doctor_char_name.setText(eachMap.get("realname"));

		} else {
			// tw_doctor_char_msg.setText("离线");
			tw_doctor_char_name.setText(eachMap.get("realname"));
		}
		return arg1;
	}
}
